//
//  SeatTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/11.
//

import UIKit



class SeatTableViewCell: UITableViewCell {

    var collectionView: UICollectionView!
    var dataArray: [ScreenModel] = []
    override init(style: UITableViewCellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.clipsToBounds = true
        self.backgroundColor = .white
        
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 10
        //UICollectionViewFlowLayoutAutomaticSize 预估高度
        //        layout.estimatedItemSize = CGSize.init(width: 200, height: 40)
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 14, y: 0, width: kScreenWidth - 28, height: 50), collectionViewLayout: layout)
        self.collectionView.register(ScreenCollectionViewCell.self, forCellWithReuseIdentifier: "ScreenCollectionViewCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = .white
        self.collectionView.showsHorizontalScrollIndicator = false
        self.contentView.addSubview(self.collectionView)
        
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(5)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(32)
            make.bottom.equalTo(-20)
        }
        
    }
    
    
    func setData(data: [ScreenModel]){
        self.dataArray = data
        self.collectionView.reloadData()
        self.collectionView.layoutIfNeeded()
        
        if let firSelect = self.dataArray.firstIndex(where: {$0.isSelect}) {
            self.collectionView.scrollToItem(at: IndexPath.init(row: firSelect, section: 0), at: .centeredHorizontally, animated: true)
        }
        
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension SeatTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let tempWidth = 90.0
//        if self.selectBrandArray.count > indexPath.row{
//            let contetModel = selectBrandArray[indexPath.row] as!  AllBrandGroupModel
//            tempWidth = XTManageCenter.getLabWidth(labelStr: contetModel.name ?? "", font: UIFont.init(name: Theme.PingFangSCRegular, size: 14)!, height: 44)
//            return CGSize.init(width: tempWidth + 30, height: 44)
//        }
        return CGSize.init(width: tempWidth, height: 32)
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ScreenCollectionViewCell", for: indexPath) as! ScreenCollectionViewCell
        
        if self.dataArray.count > indexPath.row{
            let model = self.dataArray[indexPath.row]
            cell.setSeatModel(model: model)
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.dataArray.count > indexPath.row{
            if self.dataArray.count > 0{
                let model = self.dataArray[indexPath.row]
//                model.selectSeatBlcok?(model)
                //单选
                self.dataArray.forEach { obj in
                    if model.name == obj.name{
                        model.isSelect = true
                    }else{
                        obj.isSelect = false
                    }
                }
            }
            self.collectionView.reloadData()
        }
    }
}

